package com.luchao.back.controller;

import com.luchao.back.common.constant.Execute;
import com.luchao.back.common.constant.Validate;
import com.luchao.back.common.pojo.PageResult;
import com.luchao.back.common.pojo.QueryParams;
import com.luchao.back.common.pojo.ResponseResult;
import com.luchao.back.common.service.UploadService;
import com.luchao.back.pojo.Shoes;
import com.luchao.back.service.ShoesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Api(tags = "商品(鞋子)管理")
@RestController
@RequestMapping("/shoes")
public class ShoesController {

    @Value("${file.upload.path}")
    private String path;

    @Autowired
    private ShoesService shoesService;

    @Autowired
    private UploadService uploadService;

    @ApiOperation("分页获取商品(鞋子)列表")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "queryParams", value = "分页查询参数", required = true, paramType = "formData", dataType = "QueryParams", dataTypeClass = QueryParams.class, defaultValue = "")})
    @ApiResponses(value = @ApiResponse(description = "code:响应状态码,message:返回操作消息,data:操作携带的具体数据信息"))
    @PostMapping("/getShoesByPage")
    public ResponseResult getShoesByPage(@RequestBody QueryParams queryParams) {
        if (queryParams.getPageNum() == null) queryParams.setPageNum(1);
        if (queryParams.getPageSize() == null) queryParams.setPageSize(5);
        long total = shoesService.getShoesCount(queryParams.getSizeids(), queryParams.getSbids(), queryParams.getStids(), queryParams.getScids(), queryParams.getSgenders());
        long totalPages = total % queryParams.getPageSize() == 0 ? total / queryParams.getPageSize() : (total / queryParams.getPageSize()) + 1;
        if (total <= queryParams.getPageSize()) {
            queryParams.setPageNum(1);
        }
        PageResult<Shoes> pageResult = new PageResult<Shoes>();
        pageResult.setPageNum(queryParams.getPageNum());
        pageResult.setCount(total);
        pageResult.setPage(totalPages);
        pageResult.setResult(shoesService.getShoesByPage(queryParams.getPageNum(), queryParams.getPageSize(), queryParams.getSizeids(), queryParams.getSbids(), queryParams.getStids(), queryParams.getScids(), queryParams.getSgenders()));
        return ResponseResult.success(pageResult);
    }

    @ApiOperation("分页获取商品(鞋子)类别列表")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "pageNum", value = "当前页码", required = false, paramType = "query", dataType = "Integer", dataTypeClass = Integer.class, defaultValue = "1"),
            @ApiImplicitParam(name = "pageSize", value = "当前页长度", required = false, paramType = "query", dataType = "Integer", dataTypeClass = Integer.class, defaultValue = "5")})
    @ApiResponses(value = @ApiResponse(description = "code:响应状态码,message:返回操作消息,data:操作携带的具体数据信息"))
    @PostMapping("/getShoesTypesByPage")
    public ResponseResult getShoesTypesByPage(Integer pageNum, Integer pageSize) {
        pageNum = pageNum == null ? 1 : pageNum;
        pageSize = pageSize == null ? 5 : pageSize;
        return ResponseResult.success(shoesService.getShoesTypesByPage(pageNum, pageSize));
    }

    @ApiOperation("分页获取商品(鞋子)品牌列表")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "pageNum", value = "当前页码", required = false, paramType = "query", dataType = "Integer", dataTypeClass = Integer.class, defaultValue = "1"),
            @ApiImplicitParam(name = "pageSize", value = "当前页长度", required = false, paramType = "query", dataType = "Integer", dataTypeClass = Integer.class, defaultValue = "5")})
    @ApiResponses(value = @ApiResponse(description = "code:响应状态码,message:返回操作消息,data:操作携带的具体数据信息"))
    @PostMapping("/getShoesBrandsByPage")
    public ResponseResult getShoesBrandsByPage(Integer pageNum, Integer pageSize) {
        pageNum = pageNum == null ? 1 : pageNum;
        pageSize = pageSize == null ? 5 : pageSize;
        return ResponseResult.success(shoesService.getShoesBrandsByPage(pageNum, pageSize));
    }

    @ApiOperation("分页获取商品(鞋子)颜色列表")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "pageNum", value = "当前页码", required = false, paramType = "query", dataType = "Integer", dataTypeClass = Integer.class, defaultValue = "1"),
            @ApiImplicitParam(name = "pageSize", value = "当前页长度", required = false, paramType = "query", dataType = "Integer", dataTypeClass = Integer.class, defaultValue = "5")})
    @ApiResponses(value = @ApiResponse(description = "code:响应状态码,message:返回操作消息,data:操作携带的具体数据信息"))
    @PostMapping("/getShoesColorsByPage")
    public ResponseResult getShoesColorsByPage(Integer pageNum, Integer pageSize) {
        pageNum = pageNum == null ? 1 : pageNum;
        pageSize = pageSize == null ? 5 : pageSize;
        return ResponseResult.success(shoesService.getShoesColorsByPage(pageNum, pageSize));
    }

    @ApiOperation("分页获取商品(鞋子)产地列表")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "pageNum", value = "当前页码", required = false, paramType = "query", dataType = "Integer", dataTypeClass = Integer.class, defaultValue = "1"),
            @ApiImplicitParam(name = "pageSize", value = "当前页长度", required = false, paramType = "query", dataType = "Integer", dataTypeClass = Integer.class, defaultValue = "5")})
    @ApiResponses(value = @ApiResponse(description = "code:响应状态码,message:返回操作消息,data:操作携带的具体数据信息"))
    @PostMapping("/getShoesProducerByPage")
    public ResponseResult getShoesProducerByPage(Integer pageNum, Integer pageSize) {
        pageNum = pageNum == null ? 1 : pageNum;
        pageSize = pageSize == null ? 5 : pageSize;
        return ResponseResult.success(shoesService.getShoesProducerByPage(pageNum, pageSize));
    }

    @ApiOperation("根据ID获取商品(鞋子)详情")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "sid", value = "当前页码", required = true, paramType = "query", dataType = "Integer", dataTypeClass = Integer.class, defaultValue = "")})
    @ApiResponses(value = @ApiResponse(description = "code:响应状态码,message:返回操作消息,data:操作携带的具体数据信息"))
    @PostMapping("/getShoesById")
    public ResponseResult getShoesById(Integer sid) {
        return ResponseResult.success(shoesService.getShoesById(sid));
    }

    @ApiOperation("修改商品(鞋子)信息")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "shoes", value = "商品(鞋子)对象信息", required = true, paramType = "formData", dataType = "Shoes", dataTypeClass = Shoes.class, defaultValue = "")})
    @ApiResponses(value = @ApiResponse(description = "code:响应状态码,message:返回操作消息,data:操作携带的具体数据信息"))
    @PostMapping("/editShoes")
    public ResponseResult editShoes(@RequestBody Shoes shoes) {
        return shoesService.editShoes(shoes) ? ResponseResult.success(ResponseResult.SUCCESS, "修改成功", Execute.SUCCESS) : ResponseResult.success(ResponseResult.FAILED, "修改失败", Execute.ERROR);
    }

    @ApiOperation("批量禁用/解禁商品(鞋子)")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "list", value = "商品(鞋子)列表", required = true, paramType = "formData", dataType = "List<Shoes>", dataTypeClass = List.class, defaultValue = "")})
    @ApiResponses(value = @ApiResponse(description = "code:响应状态码,message:返回操作消息,data:操作携带的具体数据信息"))
    @PostMapping("/batchChangeState")
    public ResponseResult batchChangeState(@RequestBody List<Shoes> list) {
        return shoesService.batchChangeState(list) ? ResponseResult.success(ResponseResult.SUCCESS, "操作成功", Execute.SUCCESS) : ResponseResult.success(ResponseResult.ERROR, "操作失败", Execute.ERROR);
    }

    @ApiOperation("添加商品(鞋子)")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "shoes", value = "商品(鞋子)对象信息", required = true, paramType = "formData", dataType = "Shoes", dataTypeClass = Shoes.class, defaultValue = "")})
    @ApiResponses(value = @ApiResponse(description = "code:响应状态码,message:返回操作消息,data:操作携带的具体数据信息"))
    @PostMapping("/addShoes")
    public ResponseResult addShoes(@RequestBody Shoes shoes) {
        return shoesService.addShoes(shoes) ? ResponseResult.success(ResponseResult.SUCCESS, "新增成功", Execute.SUCCESS) : ResponseResult.success(ResponseResult.FAILED, "新增失败", Execute.ERROR);
    }

    @ApiOperation("校验商品(鞋子)名称唯一性")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "sname", value = "商品(鞋子)名称", required = true, paramType = "query", dataType = "String", dataTypeClass = String.class, defaultValue = "")})
    @ApiResponses(value = @ApiResponse(description = "code:响应状态码,message:返回操作消息,data:操作携带的具体数据信息"))
    @PostMapping("/checkShoes")
    public ResponseResult checkShoes(String sname) {
        return shoesService.checkShoes(sname) > 0 ? ResponseResult.success(ResponseResult.FAILED, "名称已存在,不可以使用", Validate.ORDINARY) : ResponseResult.success(ResponseResult.SUCCESS, "名称不存在,可以使用", Validate.UNIQUE);
    }

    @ApiOperation("校验商品(鞋子)序号(SN)唯一性")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "snum", value = "商品(鞋子)序号(SN)", required = true, paramType = "query", dataType = "String", dataTypeClass = String.class, defaultValue = "")})
    @ApiResponses(value = @ApiResponse(description = "code:响应状态码,message:返回操作消息,data:操作携带的具体数据信息"))
    @PostMapping("/checkSnum")
    public ResponseResult checkSnum(String snum) {
        return shoesService.checkSnum(snum) > 0 ? ResponseResult.success(ResponseResult.FAILED, "序列号已存在,不可以使用", Validate.ORDINARY) : ResponseResult.success(ResponseResult.SUCCESS, "序列号不存在,可以使用", Validate.UNIQUE);
    }

    @ApiOperation("上传商品(鞋子)图片")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "spurl", value = "商品(鞋子)图片", required = true, paramType = "query", dataType = "MultipartFile", dataTypeClass = MultipartFile.class, defaultValue = "")})
    @ApiResponses(value = @ApiResponse(description = "code:响应状态码,message:返回操作消息,data:操作携带的具体数据信息"))
    @PostMapping("/uploadPhoto")
    public ResponseResult uploadPhoto(@RequestPart MultipartFile spurl) {
        return uploadService.uploadPhoto(path, spurl) ? ResponseResult.success(ResponseResult.SUCCESS, "上传成功", Execute.SUCCESS) : ResponseResult.success(ResponseResult.ERROR, "上传失败", Execute.ERROR);
    }

    @ApiOperation("删除商品(鞋子)图片")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "sid", value = "商品(鞋子)Id", required = true, paramType = "query", dataType = "Integer", dataTypeClass = Integer.class, defaultValue = ""),
            @ApiImplicitParam(name = "fileName", value = "商品(鞋子)图片文件名", required = true, paramType = "query", dataType = "String", dataTypeClass = String.class, defaultValue = "")})
    @ApiResponses(value = @ApiResponse(description = "code:响应状态码,message:返回操作消息,data:操作携带的具体数据信息"))
    @PostMapping("/deletePhoto")
    public ResponseResult deletePhoto(Integer sid, String fileName) {
        return shoesService.deletePhoto(path, sid, fileName) ? ResponseResult.success(ResponseResult.SUCCESS, "删除成功", Execute.SUCCESS) : ResponseResult.success(ResponseResult.ERROR, "删除失败", Execute.ERROR);
    }

    @ApiOperation("根据品牌获取商品(鞋子)列表")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "bid", value = "品牌ID", required = true, paramType = "query", dataType = "Integer", dataTypeClass = Integer.class, defaultValue = "")})
    @ApiResponses(value = @ApiResponse(description = "code:响应状态码,message:返回操作消息,data:操作携带的具体数据信息"))
    @PostMapping("/getShoesByBrands")
    public ResponseResult getShoesByBrands(Integer bid) {
        return ResponseResult.success(shoesService.getShoesByBrands(bid));
    }

    @ApiOperation("根据条件获取商品(鞋子)推荐榜列表")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "sgender", value = "性别", required = true, paramType = "query", dataType = "String", dataTypeClass = String.class, defaultValue = ""),
            @ApiImplicitParam(name = "topWay", value = "热销榜类型", required = true, paramType = "query", dataType = "String", dataTypeClass = String.class, defaultValue = "")})
    @ApiResponses(value = @ApiResponse(description = "code:响应状态码,message:返回操作消息,data:操作携带的具体数据信息"))
    @PostMapping("/getShoesTop")
    public ResponseResult getShoesTop(String sgender, String topWay) {
        return ResponseResult.success(shoesService.getShoesTop(sgender, topWay));
    }
}
